%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering
%   University of Sydney, 2011
%------------------------------------------------------------------
%------------------------------------------------------------------

% %%generates statistics about self reports
function [statExt]=stat_emoCog(subjectID,filesPath,statdir,opSys)

%Get stats from self reports

if opSys==1;
    sep='/';
elseif opSys==2
    sep='\';
end


filesDir=dir(filesPath);%dir info for files path
x=1;

for n=1:length(subjectID(:,1))    
    for m=1:length(filesDir)
        [path,name,ext,ver] = fileparts(filesDir(m).name);
        if strcmp(subjectID(n,:),name)%choose only selected subjects
            loadAnn(x,:)=[filesPath sep name ext];            
        elseif strcmp(strcat(subjectID(n,:),'_score'),name)
            loadScore(x,:)=[filesPath sep name ext];
        end        
    end
    x=x+1;
end

mkdir(statdir);

ansVec=[];
reposenseTVec=[];
emoCatVec=[];
emoSelfVec=[];
cogLevelVec=[];
cogSelfVec=[];

for j=1:(x-1)
    fid1 = fopen(loadAnn(j,:));%load annotation
    C_self = textscan(fid1,'%s%s%s%s%s%s%s%s%s%s%s%s','delimiter', ',');
    fclose(fid1);
    
    fid2 = fopen(loadScore(j,:));%load scores
    C_score = textscan(fid2,'%s%s%s%s','delimiter', ',');
    fclose(fid2);
    
    %annotation
    refTime_self=C_self{4}; %ref time in self report
    emotionCat=C_self{2}; %given emotion 
    taskLevel=C_self{3}; %given task level 
    selfTask=C_self{9}; %report task
    selfEmotion=C_self{10}; %report emotion
    submitAnsT=C_self{6};%time of task ans submission
    
    %scores
    refTime_score=C_score{1}; %ref time in score 
    score_acc=C_score{2};%correct ans
    score_sub=C_score{3};%reported ans
    responseT=C_score{4};%response time
    
    for k=1:length(refTime_score)
%         score_acc(k)
        if ~strcmp(score_acc(k),'11')
            if strcmp(score_acc(k),score_sub(k))
                ans={'correct'};
                response_time=responseT(k);
            elseif ~strcmp(score_acc(k),score_sub(k))
                ans={'wrong'};
                response_time=responseT(k);
            end
            ansVec=[ansVec;ans];
            reposenseTVec=[reposenseTVec,response_time];
            
            for l=2:length(refTime_self)
                if strcmp(refTime_self(l),refTime_score(k))
                    emoCat=emotionCat(l);
                    emoSelf=selfEmotion(l);
                    cogLevel=taskLevel(l);
                    cogSelf=selfTask(l);
                end
            end
            emoCatVec=[emoCatVec;emoCat];
            emoSelfVec=[emoSelfVec;emoSelf];
            cogLevelVec=[cogLevelVec;cogLevel];
            cogSelfVec=[cogSelfVec;cogSelf];
        end
    end
end

%rename selfEmotion
emoSelfVecL=[];
for t=1:length(emoSelfVec)
    if strcmp(emoSelfVec(t),'1')
        emoSelfL={'MV_LA'};
    elseif strcmp(emoSelfVec(t),'2')
        emoSelfL={'HV_LA'};
    elseif strcmp(emoSelfVec(t),'3')
        emoSelfL={'MV_MA'};
    elseif strcmp(emoSelfVec(t),'4')
        emoSelfL={'HV_MA'};
    elseif strcmp(emoSelfVec(t),'5')
        emoSelfL={'HV_HA'};
    elseif strcmp(emoSelfVec(t),'6')
        emoSelfL={'MV_HA'};
    elseif strcmp(emoSelfVec(t),'7')
        emoSelfL={'LV_HA'};
    elseif strcmp(emoSelfVec(t),'8')
        emoSelfL={'LV_MA'};
    elseif strcmp(emoSelfVec(t),'9')
        emoSelfL={'LV_LA'};
    else
        emoSelfL={'NoEmotion'};
    end
    emoSelfVecL = [emoSelfVecL;emoSelfL];
end

featnames = [{'Category'}, {'TaskLevel'},{'SelfEmotion'},{'SelfTask'},{'Accuracy'},{'ReponseTime'}];
featMatData=[emoCatVec,cogLevelVec,emoSelfVecL,cogSelfVec,ansVec,reposenseTVec'];
data_set = [featnames;featMatData];
CELL2CSV([statdir, sep,'taskReport.csv'],data_set,',');

clear all;
statExt=1;
